//
//  WZWebViewController.m
//  ETong
//
//  Created by wanzhao on 15/12/10.
//  Copyright (c) 2015年 wanzhao. All rights reserved.
//

#import "WZWebViewController.h"
#import "MBProgressHUD+WZ.h"


@interface WZWebViewController ()<UIWebViewDelegate>

@property (nonatomic, assign) BOOL isLoad;
@property (nonatomic, weak) UIWebView *web;
@property (nonatomic, strong) UIBarButtonItem *goBackItem;
@property (nonatomic, strong) UIBarButtonItem *goForwardItem;
@property (nonatomic, strong) UIBarButtonItem *refreshItem;

@end

@implementation WZWebViewController

- (void)dealloc
{
    NSLog(@"WZWebViewController dealloc");
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupWebView];
    
}


- (void)setupWebView {
    
    self.view.backgroundColor = WhiteColor;
    
    if (self.proDetail.name) {
        
        self.navigationItem.title = self.proDetail.name;
        
    } else if (self.proDraft.formname) {
        
        self.navigationItem.title = self.proDraft.formname;
        
    } else if (self.list.modelName) {
        
        self.navigationItem.title = self.list.modelName;
    }
    
    UIWebView *web = [[UIWebView alloc] init];
    self.web = web;
    web.frame = CGRectMake(0, 0, HARDWARE_SCREEN_WIDTH, HARDWARE_SCREEN_HEIGHT - 64.0f);
    web.delegate = self;
    [web setScalesPageToFit:YES];
    [web setDataDetectorTypes:UIDataDetectorTypeAll];
    [self.view addSubview:web];
    
//    [self setupWebViewToolBar];
    
    self.isLoad = NO;
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    [web loadRequest:self.request];
 
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//    if (self.isLoad == YES) {
//        return NO;
//    } else {
//        self.isLoad = YES;
//        return YES;
//    }
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
    self.goBackItem.enabled = webView.canGoBack;
    self.goForwardItem.enabled = webView.canGoForward;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [MBProgressHUD hideHUDForView:self.view];
    self.goBackItem.enabled = webView.canGoBack;
    self.goForwardItem.enabled = webView.canGoForward;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    [MBProgressHUD hideHUDForView:self.view];
    [MBProgressHUD showError:@"加载失败"];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (!IsIOS7) {
        self.navigationController.navigationBar.tintColor = RGBCOLORWITH16COLOR(0xa694d4);
    } else {
        self.navigationController.navigationBar.barTintColor = RGBCOLORWITH16COLOR(0xa694d4);
    }
}


- (void)setupWebViewToolBar {
    
    UIToolbar *tooBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, HARDWARE_SCREEN_HEIGHT - 44 - 64, self.view.width, 0)];
//    tooBar.backgroundColor = [UIColor redColor];
    tooBar.barStyle = UIBarStyleDefault;
    [tooBar sizeToFit];
    [self.view addSubview:tooBar];
    
    UIBarButtonItem *goBackItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(tooBarItemDidClick:)];
    
//    [goBackItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:14],NSFontAttributeName, nil] forState:UIControlStateNormal];
    goBackItem.tag = 10001;
    
    UIBarButtonItem *goForwardItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:@selector(tooBarItemDidClick:)];
    goForwardItem.tag = 10002;
    
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(tooBarItemDidClick:)];
    refreshItem.tag = 10003;
    
    UIBarButtonItem *spaceFlexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
    tooBar.items = @[goBackItem, spaceFlexItem, goForwardItem, spaceFlexItem, refreshItem];
}

- (void)tooBarItemDidClick:(UIBarButtonItem *)sender {
    
    switch (sender.tag) {
        case 10001:
            
            [self.web goBack];
            break;
            
        case 10002:
            
            [self.web goForward];
            break;
            
        case 10003:
            
            [self.web reload];
            break;
            
        default:
            break;
    }
}


// 懒加载
- (UIBarButtonItem *)goBackItem {
    
    if (_goBackItem == nil) {
        _goBackItem = [[UIBarButtonItem alloc] init];
    }
    return _goBackItem;
}


- (UIBarButtonItem *)goForwardItem {
    
    if (_goForwardItem == nil) {
        _goForwardItem = [[UIBarButtonItem alloc] init];
    }
    return _goForwardItem;
}

- (UIBarButtonItem *)refreshItem {
    
    if (_refreshItem == nil) {
        _refreshItem = [[UIBarButtonItem alloc] init];
    }
    return _refreshItem;
}
@end
